﻿using System;
using System.Data.Services.Client;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Silverlight4Netflix.ServiceReferences.NetflixCatalog;

namespace Silverlight4Netflix
{
    public partial class MainPage
    {
        private DataServiceCollection<Genre> _genres;
        private Genre _selectedGenre;
        private NetflixCatalog _context;

        public MainPage()
        {
            InitializeComponent();
            LoadingTextBlock.Opacity = 0;
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            _context = new NetflixCatalog(new Uri("http://odata.netflix.com/Catalog/"));

            _genres = new DataServiceCollection<Genre>();
            _genres.LoadCompleted += Genres_Loaded;

            var query = (from g in _context.Genres
                         orderby
                             g.Name
                         select g);
            _genres.LoadAsync(query);
        }

        private void Genres_Loaded(object sender, LoadCompletedEventArgs e)
        {
            if (_genres.Continuation != null)
            {
                _genres.LoadNextPartialSetAsync();
            }
            else
            {
                LoadingGenresLabel.Opacity = 0;
                GenreCombobox.ItemsSource = _genres;
                GenreCombobox.UpdateLayout();
            }
        }

        private void GenreCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _selectedGenre = GenreCombobox.SelectedItem as Genre;
            if (_selectedGenre != null)
            {
                if (_selectedGenre.Titles.Count == 0)
                {
                    CatalogTitleListBox.SelectedIndex = -1;
                    _selectedGenre.Titles.LoadCompleted += GenreCatalogTitles_Loaded;

                    _selectedGenre.Titles.LoadAsync();
                    LoadingTextBlock.Opacity = 1;
                }
                else
                {
                    CatalogTitleListBox.ItemsSource = _selectedGenre.Titles;
                    CatalogTitleListBox.SelectedIndex = 0;
                }
            }

        }

        private void GenreCatalogTitles_Loaded(object sender, LoadCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                LoadingTextBlock.Opacity = 1;
                LoadingTextBlock.Text = "Error loading titles!";
                return;
            }
            if (_selectedGenre.Titles.Continuation != null)
            {
                CatalogTitleListBox.ItemsSource = _selectedGenre.Titles;
                CatalogTitleListBox.UpdateLayout();
                LoadingTextBlock.Opacity = 0;
            }
            else
            {
                CatalogTitleListBox.ItemsSource = _selectedGenre.Titles;
                CatalogTitleListBox.UpdateLayout();
                MoreTitlesButton.IsEnabled = false;
                LoadingTextBlock.Opacity = 0;
            }
        }

        private void MoreTitlesButton_Click(object sender, RoutedEventArgs e)
        {
            _selectedGenre.Titles.LoadNextPartialSetAsync();
            LoadingTextBlock.Opacity = 1;
        }
    }
}
